﻿using System.Reflection;
using ApiLib.Controllers;
using ApiLib.Models;
using Furion;
using Furion.Schedule;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace ApiLib.AppStart;

/// <summary>
/// 配置类
/// </summary>
public static class GlobalConfig
{
    /// <summary>
    /// 我的配置
    /// </summary>
    public static AppCoreConfig? AppCoreConfig;

    /// <summary>
    /// 应用信息
    /// </summary>
    public static AppInfo? AppInfo;

    /// <summary>
    /// App配置
    /// </summary>
    public static AppConfig? AppConfig;

    /// <summary>
    /// 基础认证
    /// </summary>
    public static AbstractBaseAuthorization? BaseAuthorization;

    /// <summary>
    /// 返回code值
    /// </summary>
    public static BaseResponseCode BaseResponseCode = new();

    /// <summary>
    /// 子项目的配置
    /// </summary>
    public static readonly List<IBaseConfig> BaseConfigs = new();

    /// <summary>
    /// 注册服务
    /// </summary>
    public static readonly List<Action<IServiceCollection>> ConfigureServices = new();

    /// <summary>
    /// 注册服务
    /// </summary>
    public static readonly List<Action<IApplicationBuilder, IWebHostEnvironment>> Configures = new();

    /// <summary>
    /// 定时任务
    /// </summary>
    public static readonly List<Action<ScheduleOptionsBuilder>> ScheduleOptionsBuilders = new();

    /// <summary>
    /// 数据库连接
    /// </summary>
    public static readonly List<ConnectionConfig> ConnectionConfigs = new();

    /// <summary>
    /// 基础数据库连接配置
    /// </summary>
    public static readonly List<BaseConnectionConfig> BaseConnectionConfigs = new();

    /// <summary>
    /// 返回结果前回调
    /// </summary>
    public static Action<int, HttpResponse>? BaseResponse;

    /// <summary>
    /// 初始化数据库：忽略表
    /// </summary>
    public static readonly List<string> IgnoreTables = new()
        { "sys_config", "sys_upload_file", "sys_data_info", "sys_userinfo" };

    /// <summary>
    /// 路由白名单
    /// </summary>
    public static readonly List<string> RouteWhiteList = new()
        { "/swagger*", "/api/Common/Health", "/api/Common/Login", "/api/Common/Register" };

    /// <summary>
    /// 初始化数据库：忽略表属性：增加npoi的Ignore特性
    /// </summary>
    public static readonly Dictionary<string, string[]> IgnoreTableColumns = new();

    /// <summary>
    /// 文件上传目录，默认uploads
    /// </summary>
    public static string UploadPath = "uploads";

    /// <summary>
    /// 服务名称：用于微服务
    /// </summary>
    public static string ServeName = "";

    /// <summary>
    /// 数据库配置
    /// </summary>
    public static Dictionary<string, string> SystemConfigs = new();

    /// <summary>
    /// 使用wwwroot
    /// </summary>
    public static bool UseStaticFiles = false;

    /// <summary>
    /// 使用默认文件
    /// </summary>
    public static bool UseDefaultFiles = false;

    /// <summary>
    /// http请求自动跳转到https
    /// </summary>
    public static bool UseHttpsRedirection = false;

    /// <summary>
    /// 规范化结果状态码(401,403)
    /// </summary>
    public static bool UseUnifyResultStatusCodes = false;

    /// <summary>
    /// 定时任务UI
    /// </summary>
    public static bool UseScheduleUI = false;

    /// <summary>
    /// 定时任务监听
    /// </summary>
    public static bool UseDefaultSchedule = false;

    /// <summary>
    /// Banner类型：https://www.bootschool.net/ascii-art
    /// </summary>
    public static BannerType BannerType = BannerType.Coder;

    /// <summary>
    /// 是否使用数据库
    /// </summary>
    public static bool UseDb => ConnectionConfigs is { Count: > 0 };

    /// <summary>
    /// 获取Banner
    /// </summary>
    public static string Banner => BannerType switch
    {
        BannerType.Unicorn => GlobalConstants.BannerString.Unicorn,
        BannerType.Love => GlobalConstants.BannerString.Love,
        BannerType.Keyboard => GlobalConstants.BannerString.Keyboard,
        BannerType.Woman => GlobalConstants.BannerString.Woman,
        BannerType.Woman2 => GlobalConstants.BannerString.Woman2,
        BannerType.Woman3 => GlobalConstants.BannerString.Woman3,
        BannerType.Buddha => GlobalConstants.BannerString.Buddha,
        BannerType.Art => GlobalConstants.BannerString.Art,
        BannerType.Art2 => GlobalConstants.BannerString.Art2,
        BannerType.Periodic => GlobalConstants.BannerString.Periodic,
        BannerType.Coder => GlobalConstants.BannerString.Coder,
        _ => ""
    };
}

/// <summary>
/// Banner类型
/// </summary>
public enum BannerType
{
    /// <summary>
    /// 独角兽
    /// </summary>
    Unicorn,

    /// <summary>
    /// 爱情
    /// </summary>
    Love,

    /// <summary>
    /// 键盘
    /// </summary>
    Keyboard,

    /// <summary>
    /// 美女
    /// </summary>
    Woman,

    /// <summary>
    /// 美女
    /// </summary>
    Woman2,

    /// <summary>
    /// 美女
    /// </summary>
    Woman3,

    /// <summary>
    /// 佛主
    /// </summary>
    Buddha,

    /// <summary>
    /// 艺术
    /// </summary>
    Art,

    /// <summary>
    /// 艺术
    /// </summary>
    Art2,

    /// <summary>
    /// 元素周期表
    /// </summary>
    Periodic,

    /// <summary>
    /// 程序员
    /// </summary>
    Coder,
}